package com.qqtech.qquan.pub.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.BizzLevel1Enum;
import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleTopicVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.pub.enums.PubReportStatusDictEnum;
import com.qqtech.qquan.pub.model.PubReport;
import com.qqtech.qquan.pub.model.PubReportQuery;
import com.qqtech.qquan.pub.service.PubReportService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/pub/report")
public class PubReportController extends BaseWebController<PubReport, PubReportQuery> {
	@Resource
	private PubReportService pubReportService;
	@Resource
	private CommunityMessageService communityMessageService;
	@Resource
	private CircleTopicService circleTopicService;
	@Resource
	private ActivityBaseService activityBaseService;
	@Resource
	private CircleBaseService circleBaseService;
	
	@Override
	protected BaseService<PubReport> getBaseService() {
		return pubReportService;
	}
	
	/**
	 * 举报
	 * @param PubReport
	 * @return
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/doAdd")
	@ResponseBody
	public Result doAdd(@RequestBody PubReport pubReport) {
		Result result = new Result();
		if (pubReport == null) {
			result.addErr("参数不能为空");
		} else {
			if (pubReport.getReason() == null) {
				result.addErr("举报原因不能为空");
			}
			if (StringUtil.isBlank(pubReport.getContent())) {
				result.addErr("举报内容不能为空");
			}
			if (StringUtil.isBlank(pubReport.getBizzType())) {
				result.addErr("请指定举报业务类型");
			}
			if (pubReport.getBizzId() == null) {
				result.addErr("请指定举报业务Id");
			}
		}
		if (result.checkForm()) {
			//查看是否已存在举报信息
			PubReport temp = pubReportService.getOne(pubReport);
			if(temp == null){
				addCommunity4Report(pubReport);//设置-社区id
				pubReport.setCreatorId(super.getUserId());
				Integer beUserId = pubReportService.getBeUserId(pubReport.getBizzId(), pubReport.getBizzType());
				pubReport.setBeUserId(beUserId);
				pubReport.setStatus(PubReportStatusDictEnum.WAITING.getCode());
				pubReportService.insert(pubReport);
				result.setCode(RespEnum.OK.getCode());
			}else{
				//返回提示信息
				result.addErr("已举报");
			}
		}
		return result;
	}
	
    /**
     * 返回举报状态
     * @param pubReport
     * @return
     */
	@RequestMapping(value = "/getPubReport")
	@ResponseBody
	public Result getPubReport(@RequestBody PubReportQuery pubReport){
		Result result = new Result();
		//查看是否已存在举报信息
		PubReport report = pubReportService.getOne(pubReport);
		if(report != null){
			result.put("praise", report);
		}
		return result;
	}
	
	/**
	 * 附加社区id
	 * @param pubReport
	 */
	private void addCommunity4Report(PubReport pubReport){
		if(pubReport!=null && StringUtil.isNotBlank(pubReport.getBizzType())
				&& pubReport.getBizzId() != null){
			Integer bizzId = pubReport.getBizzId();
			String bizzType =pubReport.getBizzType();
			if(bizzType.equalsIgnoreCase(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode())){//社区动态
				CommunityMessageVo bizz = communityMessageService.getById(bizzId);
				if(bizz!=null && bizz.getCommunityId()!=null){
					pubReport.setCommunityId(bizz.getCommunityId());
				}
			}else if(bizzType.equalsIgnoreCase(BizzLevel2Enum.CIRCLE_TOPIC.getCode())){//圈子动态
				CircleTopicVo topic = circleTopicService.getById(bizzId);
				if(topic!=null){
					CircleBaseVo circle = circleBaseService.getById(topic.getCircleId());
					if(circle!=null && circle.getCommunityId()!=null){
						pubReport.setCommunityId(circle.getCommunityId());
					}
				}
			}else if(bizzType.equalsIgnoreCase(BizzLevel1Enum.ACTIVITY.getCode())){//活动
				ActivityBaseVo bizz = activityBaseService.getById(bizzId);
				if(bizz!=null && bizz.getCommunityId()!=null){
					pubReport.setCommunityId(bizz.getCommunityId());
				}
			}
		}
	}
}
